package thread.multi;

//不安全的买票
//线程不安全
public class UnsafeBuyTicket {

    public static void main(String[] args) {
        BuyTicket station = new BuyTicket();

        new Thread(station,"苦逼的我").start();
        new Thread(station,"牛逼的你们").start();
        new Thread(station,"可恨的黄牛党").start();
    }

}

class BuyTicket implements Runnable {

    private int tickets = 10;
    private boolean flag = true;

    @Override
    public void run() {
        while (flag) {
            buy();
        }
    }

    //synchronized同步方法，锁的是this
    public synchronized void buy() {
        if (tickets<=0) {
            flag = false;
            return;
        }
        try {
            Thread.sleep(100);
            System.out.println(Thread.currentThread().getName()+"买到票"+tickets--);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
